package com.hzncc.flowable_diboot.controller;

import com.diboot.core.binding.Binder;
import com.diboot.core.util.BeanUtils;
import com.diboot.core.util.D;
import com.diboot.core.util.V;
import com.diboot.core.vo.JsonResult;
import com.diboot.core.vo.Status;
import com.diboot.file.controller.BaseExcelFileController;
import com.diboot.file.excel.listener.ReadExcelListener;
import com.diboot.file.util.ExcelHelper;
import com.diboot.iam.annotation.BindPermission;
import com.diboot.iam.annotation.Log;
import com.diboot.iam.annotation.OperationCons;
import com.hzncc.flowable_diboot.dto.DailyReportDTO;
import com.hzncc.flowable_diboot.entity.DailyReport;
import com.hzncc.flowable_diboot.excel.listener.DailyReportImportListener;
import com.hzncc.flowable_diboot.excel.model.DailyReportExportModel;
import com.hzncc.flowable_diboot.excel.model.DailyReportImportModel;
import com.hzncc.flowable_diboot.service.DailyReportService;
import com.hzncc.flowable_diboot.service.IReportService;
import com.hzncc.flowable_diboot.vo.DailyReportListVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import jakarta.servlet.http.HttpServletResponse;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
* 日报报表 Excel处理Controller
*
* @author MyName
* @version 1.0
* @date 2024-07-09
 * Copyright © MyCorp
*/
@Slf4j
@RequestMapping("/daily-report/excel")
@RestController
@BindPermission(name = "日报报表导入导出", code = "DailyReportExcel")
public class DailyReportExcelController extends BaseExcelFileController {

    @Autowired
    private DailyReportService dailyReportService;
    @Autowired
    private IReportService reportService;
    /**
    * 预览数据
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览", businessObj = "DailyReport")
    @BindPermission(name = "导入预览", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview")
    public JsonResult preview(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreview(file, params);
    }

    /**
    * 导入预览保存
    *
    * @param params
    * @throws Exception
    */
    @Log(operation = "导入预览保存", businessObj = "DailyReport")
    @BindPermission(name = "导入预览保存", code = OperationCons.CODE_IMPORT)
    @PostMapping("/preview-save")
    public JsonResult previewSave(@RequestParam Map<String, Object> params) throws Exception {
        return super.excelPreviewSave(params);
    }

    /**
    * 上传excel并保存
    *
    * @param file
    * @param params
    * @throws Exception
    */
    @Log(operation = "直接导入", businessObj = "DailyReport")
    @BindPermission(name = "直接导入", code = OperationCons.CODE_IMPORT)
    @PostMapping("/upload")
    public JsonResult upload(@RequestParam("file") MultipartFile file, @RequestParam Map<String, Object> params) throws Exception {
        return super.uploadExcelFile(file, params);
    }

    /**
    * 下载示例文件
    *
    * @param response
    * @throws Exception
    */
    @Log(operation = "导入日报报表示例文件下载", businessObj = "DailyReport")
    @BindPermission(name = "导入日报报表示例文件下载", code = OperationCons.CODE_IMPORT)
    @GetMapping("/download-example")
    public void downloadExample(HttpServletResponse response) throws Exception{
        String fileName = "日报报表导入示例.xlsx";
        ExcelHelper.exportExcel(response, fileName, DailyReportImportModel.class, null);
    }

    /**
    * 日报报表列表导出
    *
    * @param response
    * @return
    * @throws Exception
    */
    @Log(operation = "导出列表文件", businessObj = "DailyReport")
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/export")
    public JsonResult export(DailyReportDTO dailyReportDto, @RequestParam(value = "columns", required = false) List<String> columns,
        HttpServletResponse response) throws Exception{
        //QueryWrapper<DailyReport> queryWrapper = super.buildQueryWrapperByQueryParams(dailyReportDto);
        //List<DailyReportListVO> dailyReportList = dailyReportService.getViewObjectList(queryWrapper, null, DailyReportListVO.class);
        List<DailyReport> dailyReportList = reportService.dailyReport(dailyReportDto.getCreateTimeBegin(), dailyReportDto.getCreateTimeEnd(), dailyReportDto.getName(),null);
        if (V.isEmpty(dailyReportList)) {
            return new JsonResult(Status.FAIL_OPERATION, "日报报表列表为空，导出失败");
        }
        String fileName = "日报报表列表导出_" + D.today() + ".xlsx";
        List<DailyReportExportModel> dataList = this.entityList2ExcelList(Binder.convertAndBindRelations(dailyReportList,DailyReportListVO.class));
        ExcelHelper.exportExcel(response, fileName, DailyReportExportModel.class, columns, dataList);
        return null;
    }

    /**
     * 日报报表列表导出
     *
     * @param response
     * @return
     * @throws Exception
     */
    @Log(operation = "导出列表文件", businessObj = "DailyReport")
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/checkIn/export")
    public JsonResult exportCheckIn(DailyReportDTO dailyReportDto, @RequestParam(value = "columns", required = false) List<String> columns,
                             HttpServletResponse response) throws Exception{
        //QueryWrapper<DailyReport> queryWrapper = super.buildQueryWrapperByQueryParams(dailyReportDto);
        //List<DailyReportListVO> dailyReportList = dailyReportService.getViewObjectList(queryWrapper, null, DailyReportListVO.class);
        List<DailyReport> dailyReportList = reportService.checkInReport(dailyReportDto.getCreateTimeBegin(), dailyReportDto.getCreateTimeEnd(), dailyReportDto.getName(),null);
        if (V.isEmpty(dailyReportList)) {
            return new JsonResult(Status.FAIL_OPERATION, "日报报表列表为空，导出失败");
        }
        String fileName = "签到报表列表导出_" + D.today() + ".xlsx";
        List<DailyReportExportModel> dataList = this.entityList2ExcelList(Binder.convertAndBindRelations(dailyReportList,DailyReportListVO.class));
        ExcelHelper.exportExcel(response, fileName, DailyReportExportModel.class, columns, dataList);
        return null;
    }

    @Log(operation = "导出列表文件", businessObj = "DailyReport")
    @BindPermission(name = "导出列表文件", code = OperationCons.CODE_EXPORT)
    @GetMapping("/statistics/export")
    public JsonResult exportSta(DailyReportDTO dailyReportDto, @RequestParam(value = "columns", required = false) List<String> columns,
                                    HttpServletResponse response) throws Exception{
        //QueryWrapper<DailyReport> queryWrapper = super.buildQueryWrapperByQueryParams(dailyReportDto);
        //List<DailyReportListVO> dailyReportList = dailyReportService.getViewObjectList(queryWrapper, null, DailyReportListVO.class);
        List<DailyReport> dailyReportList = reportService.statisticalReport(dailyReportDto.getCreateTimeBegin(), dailyReportDto.getCreateTimeEnd(), dailyReportDto.getName(),null);
        if (V.isEmpty(dailyReportList)) {
            return new JsonResult(Status.FAIL_OPERATION, "日报报表列表为空，导出失败");
        }
        String fileName = "日报报表统计列表导出_" + D.today() + ".xlsx";
        List<DailyReportExportModel> dataList = this.entityList2ExcelList(Binder.convertAndBindRelations(dailyReportList,DailyReportListVO.class));
        ExcelHelper.exportExcel(response, fileName, DailyReportExportModel.class, columns, dataList);
        return null;
    }

    /**
    * 获取表格导出头
    *
    * @return
    */
    @GetMapping("/export-table-head")
    public JsonResult getTableHeaderMap() {
        return new JsonResult<>(ExcelHelper.getTableHeads(DailyReportExportModel.class));
    }

    /**
     * 获取表格导出头
     *
     * @return
     */
    @GetMapping("/checkIn/export-table-head")
    public JsonResult getTableHeaderMapCheckIn() {
        return new JsonResult<>(ExcelHelper.getTableHeads(DailyReportExportModel.class));
    }

    /**
    * 实体列表转excel列表
    *
    * @param dailyReportList
    * @return
    */
    private List<DailyReportExportModel> entityList2ExcelList(List<DailyReportListVO> dailyReportList) {
        if (V.isEmpty(dailyReportList)) {
            return Collections.emptyList();
        }
        return BeanUtils.convertList(dailyReportList, DailyReportExportModel.class);
    }

    @Override
    protected ReadExcelListener<DailyReportImportModel> getExcelDataListener() {
        return new DailyReportImportListener();
    }
}